package com.alan.product.issue.service.impl;

import com.alan.product.issue.entity.ItemInventory;
import com.alan.product.issue.mapper.InventoryMapper;
import com.alan.product.issue.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void lock(List<ItemInventory> list) {
        list.sort((a,b)-> {
            long result = a.getItemId() - b.getItemId();
            return result > 0 ? 1 : (result == 0 ? 0 : -1);
        });

        for (ItemInventory itemInventory : list) {
            inventoryMapper.updateInventory(itemInventory);
        }
    }
}
